﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Enums;

namespace UIWindow.Protocols.C_Mode
{
    public class CommandFrameFormat : OmronCModeBase
    {
        /// <summary>
        /// 主机链接单元的BCD设置为0到31
        /// </summary>
        private string _unitNumber;

        public int UnitNumber
        {
            get { return int.Parse(_unitNumber); }
            set { _unitNumber = value.ToString("D2"); }
        }
        private string _headerCode;
        /// <summary>
        /// 指定两个字符
        /// </summary>
        public OmronHeaderCodeType HeaderCode
        {
            get { return (OmronHeaderCodeType)Enum.Parse(typeof(OmronHeaderCodeType), _headerCode, true); }
            set { _headerCode = value.ToString(); }
        }
        /// <summary>
        /// 地址
        /// </summary>
        public int Address { get; set; }
        public int AddressLength { get; set; }
        /// <summary>
        /// 需要写入的地址与值
        /// </summary>
        public List<int> ValueList { get; set; }
        public CommandFrameFormat() : base()
        {
            Length = 11;
        }
        public override byte[] GetStream()
        {
            _stringBuilder.Append(_unitNumber);
            _stringBuilder.Append(_headerCode);
            _stringBuilder.Append(Address.ToString("D4"));
            if (_headerCode[0] == 'R')
            {
                Length = (AddressLength * 4) + 11;
                _stringBuilder.Append(Length.ToString("D4"));
            }
            else
            {
                foreach (var item in ValueList)
                {
                    _stringBuilder.Append(item.ToString("D4"));
                }
            }
            return base.GetStream();
        }
    }
}
